var cf = require("../../../config.js");
var util = require("../../../utils/util.js");
var cusmallToken = wx.getStorageSync('cusmallToken');
var baseHandle = require("../../template/baseHandle.js");
//获取应用实例
var app = getApp();
Page(Object.assign({}, baseHandle, {

  /**
   * 页面的初始数据
   */
  data: {
    extConfig: wx.getExtConfigSync ? wx.getExtConfigSync() : {},
    app: app,
    staticResPath: cf.config.staticResPath,
    userImagePath: cf.config.userImagePath,
    id: "",
    name: "",
    compyName: "",
    languageDesc:"",
    mLanguageDesc:"",
    languageTplShow:false,
    placeholderTxt:"这里输入分享名片描述",
    isLoading:false,
    textareaShow:true,
    language:[
      "欢迎来参观NN的个人主页"
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(app.globalData)
    wx.hideShareMenu();
    var ctx = this;
    wx.setNavigationBarTitle({
      title: app.globalData.cardTitle+'分享语',
    })
    app.getUserInfo(this, options, function (userInfo, res) {
        cusmallToken = wx.getStorageSync('cusmallToken');
	    var companyInfo = wx.getStorageSync('companyInfo');
	    ctx.setData({
	    	id: options.id,
	    	name: options.name,
	    	compyName: companyInfo.shortName,
	    	languageDesc: options.shareSpeech,
        headPic:options.headPic
	    });
	    for (var i=0; i<ctx.data.language.length; i++) {
	    	ctx.data.language[i] = ctx.data.language[i].replace("CC", ctx.data.compyName)
	    		.replace("NN", ctx.data.name);
	    }
	    ctx.setData({
	    	language: ctx.data.language
	    });
	    if ("null" == ctx.data.languageDesc) {
	    	ctx.setData({
	    		languageDesc: ctx.data.language[0]
	    	})
	    }
    });
  },
  showLanguageTpl(){
    var ctx = this;
    ctx.setData({
      languageTplShow: !ctx.data.languageTplShow
    });
    if(ctx.data.languageTplShow){
      ctx.setData({
        mLanguageDesc: ctx.data.languageDesc,
        languageDesc:"",
        textareaShow:false
      });
    }else{
      ctx.setData({
        languageDesc: ctx.data.mLanguageDesc,
        textareaShow: true,
      });
    }
  },
  selectSen(e){
    var idx = e.currentTarget.dataset.idx;
    var ctx  = this;
    ctx.setData({
      languageDesc: ctx.data.language[idx],
      languageTplShow: false,
      textareaShow: true
    });
  },
  languageInput(e){
    var ctx = this;
    ctx.setData({
      languageDesc:e.detail.value
    });
  },
  languageCommit(e){
    var ctx = this;
    if (0 === ctx.data.languageDesc.length){
      wx.showToast({
        title: '分享名片描述不能为空',
        icon:"none"
      });
      return;
    }
    if (ctx.data.isLoading){
      wx.showToast({
        title: '不要重复提交',
        icon: "none"
      });
      return;
    }
    ctx.setData({
      isLoading:true
    });
    wx.showLoading({
      title: '提交中'
    });
    wx.request({
      url: cf.config.pageDomain + "/applet/mobile/visitcard/employee/modifyEmployeeInfo",
      data: {
        cusmallToken: cusmallToken,
        id: ctx.data.id,
        shareSpeech: ctx.data.languageDesc
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var data = res.data;
        wx.hideLoading()
        if (data && 0 === data.ret) {
          wx.showModal({
            title: '提示',
            content: '保存成功',
            showCancel: false,
            success(){
              setTimeout(function(){
                let pages = getCurrentPages();//当前页面
                let prevPage = pages[pages.length - 2];//上一页面
                prevPage.setData({//直接给上移页面赋值
                  "cardInfo.shareSpeech": ctx.data.languageDesc
                });
                wx.navigateBack({
                  delta: 1
                });
              },1000);
            }
          });
        }else{
	      wx.showModal({
            title: '提示',
            content: '保存失败',
            showCancel: false
          })
        }
      },
      fail() {
        wx.hideLoading()
      },
      complete() {
        ctx.setData({
          isLoading: false
        });
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
}))